﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Linq.Expressions;
using Word = Microsoft.Office.Interop.Word;
using System.IO;

public partial class Manage_StatTrain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 绑定年、月、日
            for (int i = 2012; i <= 2007 + 10 || i <= DateTime.Now.Year + 2; i++)
            {
                Year1.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
                Year2.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            for (int i = 1; i <= 12; i++)
            {
                Month1.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
                Month2.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            for (int i = 1; i <= 31; i++)
            {
                Day1.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
                Day2.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }

            Year2.SelectedValue = Convert.ToString(DateTime.Now.Year);
            Month2.SelectedValue = Convert.ToString(DateTime.Now.Month);
            Day2.SelectedValue = Convert.ToString(DateTime.Now.Day);
            #endregion

            #region 绑定县区
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Department.xml");
            xds.XPath = "//District";

            Districts.DataSource = xds;
            Districts.DataTextField = "name";
            Districts.DataValueField = "name";
            Districts.DataBind();
            #endregion
        }
    }
    protected void Export_Click(object sender, EventArgs e)
    {
        DateTime st = DateTime.Parse(string.Format("{0}年{1}月{2}日", Year1.SelectedValue, Month1.SelectedValue, Day1.SelectedValue));
        DateTime ed = DateTime.Parse(string.Format("{0}年{1}月{2}日", Year2.SelectedValue, Month2.SelectedValue, Day2.SelectedValue));

        Word.Application app = null;
        Word.Document doc = null;

        object ms = Type.Missing;
        
        try
        {
            StringBuilder sb = new StringBuilder();
            foreach (ListItem li in Districts.Items)
            {
                if (li.Selected)
                {
                    sb.Append(" " + li.Value);
                }
            }

            CmsDataContext db = new CmsDataContext();
            var q = from aT in db.Train
                    orderby aT.Started
                    where aT.Started.CompareTo(st) >= 0 && ed.CompareTo(aT.Started) >= 0
                    select aT;

            #region 查询条件
            Expression<Func<Train, bool>> lmbda = PredicateBuilder<Train>.CreatePredicate(new string[] { "District" }, sb.ToString().Trim(), "aT");
            q = q.Where(lmbda);
            #endregion

            string path = Toolkit.MappingPath(Toolkit.PathOfTemporary);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string file = Toolkit.NextUUID + ".doc";
            File.Copy(Toolkit.MappingPath(string.Format("{0}/{1}", Toolkit.PathOfReport, Toolkit.TemplateOfStatTrain)), string.Format("{0}/{1}", path, file));
            
            app = new Word.Application();
            app.Visible = true;
            doc = app.Documents.Open(string.Format("{0}/{1}", path, file));
            
            Word.Bookmark bm = doc.Bookmarks["Time"];
            bm.Range.Text = string.Format("{0:yyyy年MM月dd日}", DateTime.Now);
            
            bm = doc.Bookmarks["Person"];
            bm.Range.Text = Toolkit.CurrentUserName;
            
            bm = doc.Bookmarks["During"];
            bm.Range.Text = string.Format("{0:yyyy年MM月dd日}-{1:yyyy年MM月dd日}", st, ed);
            
            Word.Row row;
            Word.Cell cell;
            Word.Table tbl = doc.Tables[1];

            int iRow = 0;
            foreach (Train t in q)
            {
                iRow++;
                row = tbl.Rows.Add();

                cell = row.Cells[1];
                cell.Range.Text = Convert.ToString(iRow);

                cell = row.Cells[2];
                cell.Range.Text = t.District;

                cell = row.Cells[3];
                cell.Range.Text = t.Belong;

                cell = row.Cells[4];
                cell.Range.Text = t.Name;

                cell = row.Cells[5];
                cell.Range.Text = t.Teacher.Name;

                cell = row.Cells[6];
                cell.Range.Text = string.Format("{0:yyyy年MM月dd日 HH:mm}", t.Started);

                cell = row.Cells[7];
                cell.Range.Text = t.Address;

                cell = row.Cells[8];
                cell.Range.Text = t.Course;

                cell = row.Cells[9];
                cell.Range.Text = string.Format("{0}/{1}", t.Amount, t.JoinTrain.Count);

                cell = row.Cells[10];
                cell.Range.Text = t.Memo;
            }

            doc.Save();
            
            doc.Close(ref ms, ref ms, ref ms);

            doc = null;

            Response.Redirect(string.Format("~/Temps/{0}", file));
        }
        catch (Exception ee)
        {
            Response.Write(ee.Source);
            Response.Write(ee.StackTrace);
            Response.Write(ee.Message);
        }
        finally
        {
            if (app != null)
            {
                app.Quit(ref ms, ref ms, ref ms);
                app = null;
            }
        }
    }
}